package com.nidu.demo.web.oauth2;

import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson2.JSONArray;
import com.nidu.demo.base.BatchDeleteCmd;
import com.nidu.demo.oauth2.dto.OAuth2RefreshTokenPageQry;
import com.nidu.demo.oauth2.api.OAuth2RefreshTokenService;
import com.nidu.demo.oauth2.dto.OAuth2RefreshTokenCO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;

/**
 * OAuth2刷新令牌管理 Controller
 */
@Tag(name = "管理后台 - OAuth2刷新令牌")
@RestController
@RequestMapping("/oauth2/refresh-tokens")
@Validated
@RequiredArgsConstructor
public class OAuth2RefreshTokenController {

    private final OAuth2RefreshTokenService service;

    @Operation(summary = "删除OAuth2刷新令牌", description = "根据 ID 删除OAuth2刷新令牌")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('oauth2:refresh-token:delete')")
    public Response deleteAccessToken(@PathVariable Long id) {
        return service.delete(id);
    }

    @Operation(summary = "批量删除OAuth2刷新令牌", description = "根据 ID 列表删除多个OAuth2刷新令牌")
    @PostMapping("/batch")
    @PreAuthorize("@ss.hasPermission('oauth2:refresh-token:delete')")
    public Response deleteAccessTokens(@RequestBody BatchDeleteCmd cmd) {
        List<Long> ids = JSONArray.parseArray(cmd.getDelete(), Long.class);
        return service.delete(new HashSet<>(ids));
    }

    @Operation(summary = "获取OAuth2刷新令牌详情", description = "根据 ID 获取OAuth2刷新令牌详细信息")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('oauth2:refresh-token:query')")
    public SingleResponse<OAuth2RefreshTokenCO> getAccessTokenById(@PathVariable Long id) {
        return service.getById(id);
    }

    @Operation(summary = "分页查询OAuth2刷新令牌", description = "分页获取OAuth2刷新令牌信息")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('oauth2:refresh-token:query')")
    public PageResponse<OAuth2RefreshTokenCO> pageAccessToken(OAuth2RefreshTokenPageQry qry) {
        return service.page(qry);
    }
}
